<template>
	<view class="tabs">
		<view class="top box">
			<view class="iconfont iconfanhui flex-1" @tap="back">
			</view>
			<view class="title flex-1">
				我发布的
			</view>
			<view  class="icon flex-1 box d-ai-c box">
				<view class="box d-ai-c flex-3 address-box d-jc-end" v-if="userInfo.userInfo.roles.indexOf('teacher_leader')>-1"  @tap="allissue">
					发布通知
				</view>
			</view>
		</view>
		<view v-if="isNull" class="cart_null">
			<image src="../../static/images/xiaoxi@2x.png" mode=""></image>
			<text>暂时还没有发布通知消息~</text>
		</view>
		<scroll-view v-if="!isNull" :refresher-triggered=refresherTriggered :refresherThreshold=refresherThreshold
				@refresherrefresh='refresherrefresh()' refresher-background="#FFFFFF"  @scrolltolower='nextPage()'
				 
				 lower-threshold="500" class="" scroll-y :style="{height: orderHeight+'px'}">
			<view class="scroll">
				<view class="" v-for="(item,index) in MessageFromList" :key="index" @tap="jumpList(item.id)">
					<view class="c-t" >
						{{item.createTime}}
					</view>
					<view class="content" >
						<view class="content_1">
							<view class="">
								{{item.title}}
							</view>
							<view class="">
								{{item.readTotal}}人已读
							</view>
						</view>
						<view class="content_2">
							{{item.content}}
						</view>
						<view v-if="item.toClassifyName!==null" class="content_3">
							发送到：{{item.toShopName}}>{{item.toClassifyName}}>{{item.toClassName}}
						</view>
						<view class="content_3" v-if="item.toClassifyName===null">
							发送到：{{item.toShopName}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 教务通知弹框 --> 
		<wybPopup ref="popup" type="bottom">
		    <view>
				<view style="height: 100rpx;background-color: #FFFFFF;" class="page-section-spacing pop_top">
					<view class="scroll-view-item_H" v-for="(item,index) in tabList" @tap="changeTab(index)">
						<view class="animate__animated" hover-class="animate__rubberBand"
							:class="index == isActive ? 'active' :''">
							{{item.name}}
						</view>
						<view class="" :class="index == isActive ? 'line1' :''"></view>
					</view>
				</view>
				<view class="pop" v-if="this.isActive===0">
					<scroll-view scroll-y="true" :style="{height: Height+'px'}">
						<view>
							<view class="popbox">
								<view class="p_t">
									<text>
										标题：
									</text>
								</view>
								<view class="">
									<textarea v-model="titlecontent"  class="textarea1 m-1" placeholder="请输入标题内容(内容长度不超过30个字符)" :maxlength="30" auto-height />
									
								</view>
							</view>
							<view class="popbox">
								<view class="p_t" >
									<text>
										内容：
									</text>
								</view>
								<view class="">
									<textarea v-model="detailcontent"  class="textarea m-1" placeholder="请输入详细通知内容(内容长度不超过200个字符)" :maxlength="200" auto-height />
									
								</view>
							</view>
						
						</view>
					</scroll-view>
					<view class="btn" @tap="send(1)">
						确定发送
					</view>
				</view>
				<view class="pop" v-if="this.isActive===1">
					<scroll-view scroll-y="true" :style="{height: Height+'px'}">
						<view>
							<view class="popbox">
								<view class="p_t">
									<text>
										标题：
									</text>
								</view>
								<view class="">
									<textarea v-model="titlecontent"  class="textarea1 m-1" placeholder="请输入标题内容(内容长度不超过30个字符)" :maxlength="30" auto-height />
									
								</view>
							</view>
							<view class="popbox">
								<view class="p_t" >
									<text>
										内容：
									</text>
								</view>
								<view class="">
									<textarea v-model="detailcontent"  class="textarea m-1" placeholder="请输入详细通知内容(内容长度不超过200个字符)" :maxlength="200" auto-height />
									
								</view>
							</view>
							
						</view>
					</scroll-view>
					<view class="btn" @tap="send(2)">
						确定发送
					</view>
				</view>
				<view class="pop" v-if="this.isActive===2">
					<scroll-view scroll-y="true" :style="{height: Height+'px'}">
						<view>
							<view class="popbox">
								<view class="">
									<text>
										选择系别、班级：
									</text>
									
								</view>
								<olylis-cascader :options="treeData" @change="handleChange" :value="selectedValue"  :show="show"></olylis-cascader>
							</view>
							<view class="popbox">
								<view class="p_t" >
									<text>
										标题：
									</text>
								</view>
								<view class="">
									<textarea v-model="titlecontent"  class="textarea1 m-1" placeholder="请输入标题内容(内容长度不超过30个字符)" :maxlength="30" auto-height :is-show-nav="false"/>
									
								</view>
							</view>
							<view class="popbox">
								<view class="p_t" >
									<text>
										内容：
									</text>
								</view>
								<view class="">
									<textarea v-model="detailcontent"  class="textarea m-1" placeholder="请输入详细通知内容(内容长度不超过200个字符)" :maxlength="200" auto-height />
									
								</view>
							</view>
							
						</view>
					</scroll-view>
					<view class="btn" @tap="send(3)">
						确定发送
					</view>
				</view>
			</view>
		
	 </wybPopup>
	</view>
</template>

<script>
	import olylisCascader from '@/components/olylis-cascader/olylis-cascader.vue'
	import wybPopup from '@/components/wyb-popup/wyb-popup.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			olylisCascader,
			wybPopup
		},
		computed: mapState(['loginState', 'userInfo']),
		data() {
			return {
				isNull: false,
				pageNum:1,
				pageSize:10,
				hasNextPage: true,
				refresherThreshold: 50,
				refresherTriggered: false,
				_refresherTriggered: false,
				Height:350,
				orderHeight: '',
				MessageFromList:[],
				toClassId:'',
				toClassifyId:'',
				selectedValue: [],
				treeData: [],
				detailcontent:'',
				titlecontent:'',
				flag:'',
				show:false,
				isActive: 0,
				tabList: [{
						id: "1",
						name: '通知全校'
					}, {
						id: "2",
						name: '通知老师'
					},
					{
						id: "3",
						name: '通知学员'
					},
				],
			}
		},
		onReady() {
			let that = this;
			uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
				success(res) { //成功回调函数
					that.orderHeight = res.windowHeight - uni.upx2px(165)
					//windoHeight为窗口高度，主要使用的是这个

				}
			})
		},
		onLoad(){
			this.getMessageFromList()
			// this.getUserInfo()
			
		},
		onShow() {
			// this.getUserInfo()
			this.userInfo
		
		},
		methods: {
			jumpList(id){
				uni.navigateTo({
					url:'../teacher/msgInfo?id='+id
				})
			},
			refresherrefresh() {
				console.log("自定义下拉刷新被触发");
				let _this = this;
				if (_this._refresherTriggered) {
					return;
				}
				_this._refresherTriggered = true;
				//界面下拉触发，triggered可能不是true，要设为true
				if (!_this.refresherTriggered) {
					_this.refresherTriggered = true;
				}
				// _this.getClassOrdersList(_this.payState)
				setTimeout(() => {
					_this.refresherTriggered = false; //触发onRestore，并关闭刷新图标
					_this._refresherTriggered = false;
				}, 1500)
			
			},
			nextPage() {
				console.log("====出发底部===")
				console.log("====出发底部===" + this.pageNum)
				console.log("====出发底部===" + this.hasNextPage)
				if (this.hasNextPage) {
					this.pageNum = this.pageNum + 1
					console.log(this.pageNum)
					this.$api.getMessageFromList({
					},this.pageNum, this.pageSize).then((res) => {
						if (res.data.code == 200) {
							console.log(res.data)
							// this.MessageToList = res.data.dataMap
							var arr = res.data.dataMap.list
							// for (var i = 0; i < arr.length/2; i++) {
							// var temp = arr[i]; //交换变量
							// arr[i] = arr[arr.length - i - 1];
							// arr[arr.length-i-1]=temp;
							// }
							// console.log(arr)
							// this.MessageToList = arr
							for (var i = 0; i < arr.length; i++) {
								this.MessageFromList.push(arr[i])
							}
							this.hasNextPage = res.data.dataMap.hasNextPage
						}
					})
				}
				
			
			},
			allissue() {
				console.log(1)
				this.$refs.popup.show() 
				this.titlecontent= ''
				this.detailcontent= ''	
				this.getClassOrActivityList()
				this.changeTab(0)
				
				
			
			},
			back(){
				uni.navigateBack(1)
			},
			handleChange(selectedValue, clickItem) {
			    var classInfo = ('handleChange', selectedValue, clickItem)
				console.log(classInfo)
				
			    this.selectedValue = selectedValue
				this.toClassifyId = selectedValue[0]
				this.toClassId = selectedValue[1]
				console.log(this.toClassifyId)
				console.log(this.toClassId)
			},
			getMessageFromList(){
				this.pageNum = 1
				this.$api.getMessageFromList({
					
				},this.pageNum, this.pageSize).then((res) => {
					if (res.data.code == 200) {
						console.log(res.data)
						var arr = res.data.dataMap.list
						// for (var i = 0; i < arr.length/2; i++) {
						// var temp = arr[i]; //交换变量
						// arr[i] = arr[arr.length - i - 1];
						// arr[arr.length-i-1]=temp;
						// }
						// console.log(arr)
						this.MessageFromList = arr
						this.hasNextPage = res.data.dataMap.hasNextPage
						this.isNull = this.MessageFromList.length == 0 ? true : false
					}
				})
			},
			//点击事件改变swiper
			changeTab(active) {
				this.isActive = active
				if (active == 0) {
					this.payState = ''
					console.log(this.isActive)
					this.titlecontent= ''
					this.detailcontent= ''	
				} else if (active == 1) {
					this.studentlist = ''
					this.titlecontent= ''
					this.detailcontent= ''	
				} else if (active == 2) {
					// this.payState = 0
					// this.handleChange()
					this.titlecontent= ''
					this.detailcontent= ''
					this.selectedValue=[]
					console.log(this.selectedValue)
				}
			},
			//发送通知
			send(flag){
				if(flag===1){
					this.flag = 1
				}else if(flag===2){
					this.flag = 2
				}else if(flag===3){
					this.flag = 3
				}
				console.log(this.toClassifyId)
				console.log(this.selectedValue)
				if(this.titlecontent===''||this.detailcontent===''){
					uni.showToast({
						title: '请填写完整信息！',
						duration: 1500,
						icon: "none",
						
					});
					return
				}else{
				this.$api.sendMessage({
					title: this.titlecontent,
					content: this.detailcontent,
					role:1,
					flag:this.flag,
					toClassifyId: this.toClassifyId,
					toClassId: this.toClassId
				}).then((res) => {
					if (res.data.code == 200) {
						this.changeTab(0)
						this.treeData = []
						uni.showToast({
							title: '发送成功！',
							duration: 1500,
							icon: "none",
							
						});
						this.$refs.popup.hide();
						setTimeout(()=> {
						          this.getMessageFromList()
						     }, 1000);
					}
					
				})
				}
			},
			//获取该学校系别、班级
			getClassOrActivityList(){
				console.log(this.userInfo.userInfo.teacherLeaderShopIds[0])
				this.$api.getClassOrActivityList({
					shopId:this.userInfo.userInfo.teacherLeaderShopIds[0]
				}).then((res) => {
					if (res.data.code == 200) {
						console.log(res.data.dataMap)
						this.treeData = []
						// this.ceshi = res.data.dataMap
						for(var i = 0;i<res.data.dataMap.length;i++){
							this.treeData.push({
								value: i,
								label: '夕阳红老年学堂2',
								children:[]
							})
							for(var j = 0;j<res.data.dataMap[i].data.length;j++){
								this.treeData[i].children.push({
									value: j,
									label: '夕阳红老年学堂2'
								})
							}
						}
						for(var i = 0;i<res.data.dataMap.length;i++){
							this.treeData[i].label = res.data.dataMap[i].classifyName
							this.treeData[i].value = res.data.dataMap[i].id
							for(var j = 0;j<res.data.dataMap[i].data.length;j++){
								this.treeData[i].children[j].label = res.data.dataMap[i].data[j].className
								this.treeData[i].children[j].value = res.data.dataMap[i].data[j].id
							}
						}
						console.log(this.treeData)
						
					}
					
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.cart_null {
		display: flex;
		width: 100%;
		flex-direction: column;
		background-color: #FFFFFF;
	}
	
	.cart_null text {
		margin: 0 auto;
		color: #666666;
	}
	
	.cart_null image {
		margin: 60rpx auto;
		width: 250rpx;
		height: 250rpx;
	}
	.top {
		color: #fff;
		padding: var(--status-bar-height) 20rpx 20rpx 20rpx;
		background-color: rgb(255, 85, 81);
	
		.iconfanhui {
			font-size: 32rpx;
		}
	
		.title {
			text-align: center;
			font-size: 32rpx;
		}
	}
	.p_t>text{
		font-size: 30rpx;
	}
	.scroll{
		overflow: hidden;
		padding-bottom: 40rpx;
	}
	.tabs {
		flex: 1;
		flex-direction: column;
		background-color: #F8F8F8;
		overflow: hidden;
	}
	.c-t{
		width: 690rpx;
		height: 60rpx;
		line-height: 60rpx;
		margin: 0rpx auto;
		text-align: center;
		font-size: 22rpx;
		font-weight: 500;
		color: #999999;
	}
	.content {
		width: 690rpx;
		padding-bottom: 18rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: 0rpx auto;
		overflow: hidden;
	}

	.content_1 {
		width: 630rpx;
		height: 70rpx;
		margin: 0 auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 10rpx;
	}

	.content_1>view:nth-child(1) {
		width: 400rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.content_1>view:nth-child(2) {
		font-size: 26rpx;
		font-weight: bold;
		color: #FF5551;
	}
	.content_2 {
		width: 630rpx;
		margin: 0 auto;
		line-height: 36rpx;
		font-size: 30rpx;
		color: #666666;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.content_3 {
		width: 630rpx;
		margin: 0 auto;
		margin-top: 18rpx;
		font-size: 22rpx;
		font-weight: 500;
		color: #999999;
	}
	.popbox {
		width: 650rpx;
		margin: 0 auto;
	
	}
	.textarea1 {
		width: 590rpx;
		background-color: #F8F8F8;
		min-height: 100rpx;
		border-radius: 10rpx;
		font-size: 28rpx;
		padding: 25rpx 20rpx;
	}
	.btn {
		width: 85%;
		height: 100rpx;
		border-radius: 50rpx;
		background-color: #FF5551;
		margin: 0 auto;
		color: #FFFFFF;
		text-align: center;
		line-height: 100rpx;
		margin-top: 50rpx;
	}
	.textarea {
		width: 590rpx;
		background-color: #F8F8F8;
		min-height: 160rpx;
		border-radius: 10rpx;
		font-size: 28rpx;
		padding: 25rpx 20rpx;
	}
	.poptext {
		height: 120rpx;
		line-height: 120rpx;
		margin-top: 50rpx;
	}
	.page-section-spacing {
		background-color: #f3f3f3;
		display: flex;
		justify-content: space-around;
	}
	
	.line1 {
		height: 8rpx;
		background-color: #FF5551;
		width: 50%;
		margin: auto;
		border-radius: 50rpx;
		margin-top: 5rpx;
	}
	
	.line-gray {
		height: 1rpx;
		width: 660rpx;
		background-color: #f0f0f0;
		margin: auto;
		border-radius: 24rpx;
		z-index: 99;
	}
	
	.scroll-view_H {
		white-space: nowrap;
		width: 90%;
	
	}
	
	.scroll-view-item_H>view {
		font-size: 33rpx;
		/* border: 1px solid red; */
		line-height: 80rpx;
	
	}
	.scroll-view-item_H {
		display: inline-block;
		width: 150rpx;
		height: 75rpx;	
		text-align: center;
		font-size: 34rpx;
		/* border: 1px solid red; */
		/* margin-left: 28rpx; */
	}
	.active {
		color: #a7a7a7;
	}
	.pop_top{
		padding: 20rpx 0 20rpx 0;
		border-radius: 60rpx 60rpx 0rpx 0rpx;
	}
	.pop{
		padding-bottom: 50rpx;
	}
</style>
